package com.cungmua.app.base;

import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.view.ViewStub;
import android.widget.EditText;
import android.widget.ExpandableListView;

import com.cungmua.app.R;
import com.cungmua.app.adapter.BaseExpandableListViewAdapter;
import com.cungmua.app.handler.DrawerHandler;
import com.cungmua.app.model.Cart;
import com.cungmua.app.model.Category;
import com.cungmua.app.model.City;
import com.cungmua.app.model.DrawerChild;
import com.cungmua.app.model.DrawerParent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by kelvin.nguyen on 6/17/14.
 */
public abstract class BaseDrawerActivity extends BaseAuthenticatedActivity implements DrawerHandler,ExpandableListView.OnChildClickListener,ExpandableListView.OnGroupClickListener{
    public static final String LOGIN = "login";
    public static final String LOGOUT = "logout";
    public static final String CART = "cart";
    public static final String REGISTER = "register";
    public static final String PROFILE = "profile";
    public static final String CHANGE_PASSWORD = "change_password";
    public static final String PAST_ORDERS = "pass_orders";
    public static final String BONUS_POINT = "bonus_point";
    public static final String FAVORITE_DEALS = "favorite_deals";
   // private ActionBarDrawerToggle mDrawerToggle;
    protected DrawerLayout drawerLayout;
    private String appName = " ";
    private int appIcon;
    private String lastTitle = " ";
    private int ParentClickStatus = -1;
    private int ChildClickStatus = -1;
    protected BaseExpandableListViewAdapter listDrawerAdapter;
    ExpandableListView expandableListView;
    protected DrawerParent cartDrawerItem;
    protected DrawerParent searchDrawerItem;

    @Override
    protected void initActivity(Bundle savedInstanceState) {
        setContentView(R.layout.drawer_container);
        ViewStub stub = (ViewStub) findViewById(R.id.stub_main_content);
        stub.setLayoutResource(getLayoutResource());
        stub.inflate();

        loadControls(savedInstanceState);
        updateView();
        prepareDrawer();

        if (getCurrentCity() == null) {
            goToChooseCity();
        }
    }


    @Override
    public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPos, int pos, long l) {
        DrawerChild current = listDrawerAdapter.getChild(groupPos, pos);
        if (current.getData() instanceof City) {
            setCurrentCity((City) current.getData());
            setCurrentCart(new Cart());
            goToHotDeal(true);
        }
        if (current.getData() instanceof Category) {
            drawerLayout.closeDrawers();
            goToCategory(((Category) current.getData()).getId(), ((Category) current.getData()).getName());
        }
        return false;
    }

    @Override
    public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPos, long l) {
        DrawerParent current = listDrawerAdapter.getGroup(groupPos);
        if (current.getData() instanceof Category) {
            Category cate = (Category) current.getData();

            if (cate.getChildren() == null) { // not expandable
                drawerLayout.closeDrawers();
                goToCategory(cate.getId(), cate.getName());
            }
        }
        if (groupPos == 3) { // Hot deal
            drawerLayout.closeDrawers();
            goToHotDeal(false);
        }
        if (current.getData() instanceof String) {
            String command = (String) current.getData();

            if (command.equals(LOGIN)) {
                login();
            } else if (command.equals(CART)) {
//                goToYourCart();
            } else if (command.equals(LOGOUT)) {
//                logOut();
            } else if (command.equals(REGISTER)) {
//                register();
            } else if (command.equals(PROFILE)) {
//                goToProfile();
            } else if (command.equals(BONUS_POINT)) {
//                goToBonus();
            } else if (command.equals(FAVORITE_DEALS)) {
//                goToFavorite();
            } else if (command.equals(PAST_ORDERS)) {
//                goToPastDeals();
            } else if (command.equals(CHANGE_PASSWORD)) {
//                changePassword();
            }
        }
        return false;
    }

    List<DrawerParent> listDataHeader;
    HashMap<Integer, List<DrawerChild>> listDataChild;

    private void prepareDrawer() {

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);



        expandableListView = (ExpandableListView) findViewById(R.id.expandableListViewMenu);
        prepareListData();
        listDrawerAdapter = new BaseExpandableListViewAdapter(
                this, listDataHeader, listDataChild);

        expandableListView.setAdapter(listDrawerAdapter);
        expandableListView.setOnChildClickListener(this);
        expandableListView.setOnGroupClickListener(this);


    }

    private void prepareListData() {

        listDataHeader = new ArrayList<DrawerParent>();
        listDataChild = new HashMap<Integer, List<DrawerChild>>();

        listDataHeader.add(new DrawerParent(getString(R.string.all), "",ICON_DRAW_HOME, false, false,
                false));

        listDataHeader.add(new DrawerParent(getString(R.string.promotions), "",ICON_DRAW_HOME_PROMOTIONS, false, false,
                false));

        // Adding child data

    //    prepareSearchBox();

      //  prepareCart();

        listDataHeader.add(new DrawerParent(getString(R.string.danhmuc), "", 0, false, true,
                false));

        prepareCategory();

        prepareUser();

        listDataHeader.add(new DrawerParent(getString(R.string.city), "", 0, false, true,
                false));
        // Location
        prepareLocation();
    }

    private void prepareCart() {
        Cart currentCart = getCurrentCart();
        cartDrawerItem = new DrawerParent(getString(R.string.cart), String.valueOf(currentCart.getTotalQuantity()),
                0, false, false, false);
        cartDrawerItem.setData(CART);
        listDataHeader.add(cartDrawerItem);
    }
    private void prepareSearchBox(){
        EditText search=new EditText(this);
        search.setHint("Search deal...");

//        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) search.getLayoutParams();
//        layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
//        search.setLayoutParams(layoutParams);

        searchDrawerItem=new DrawerParent(search,"",0,false,false,false);
        searchDrawerItem.setData(CART);
        listDataHeader.add(searchDrawerItem);
    }

    private void prepareUser() {
        if (getCurrentUser().getId() == 0) { // default user
            DrawerParent loginDrawerItem = new DrawerParent(getString(R.string.login), "", ICON__DRAW_LOGIN, false,
                    false, false);
            loginDrawerItem.setData(LOGIN);
            listDataHeader.add(loginDrawerItem);

            loginDrawerItem = new DrawerParent(getString(R.string.register), "", ICON__DRAW_REGISTER, false,
                    false, false);
            loginDrawerItem.setData(REGISTER);
            listDataHeader.add(loginDrawerItem);
        } else {
            listDataHeader.add(new DrawerParent(getCurrentUser().getName(), "", 0, false,
                    true, false));
            DrawerParent drawerItem = new DrawerParent(getString(R.string.draw_profile), "", 0, false, false, false);
            drawerItem.setData(PROFILE);
            listDataHeader.add(drawerItem);

            drawerItem = new DrawerParent(getString(R.string.change_password), "", ICON_DRAW_FORGOTPASS, false, false, false);
            drawerItem.setData(CHANGE_PASSWORD);
            listDataHeader.add(drawerItem);

            drawerItem = new DrawerParent(getString(R.string.transation_history), "", ICON_DRAW_HISTORY, false, false, false);
            drawerItem.setData(PAST_ORDERS);
            listDataHeader.add(drawerItem);

            drawerItem = new DrawerParent(getString(R.string.award_money), "",ICON_DRAW_MONEY, false, false, false);
            drawerItem.setData(BONUS_POINT);
            listDataHeader.add(drawerItem);

            drawerItem = new DrawerParent(getString(R.string.favorite), "", ICON_DRAW_LOVE, false, false, false);
            drawerItem.setData(FAVORITE_DEALS);
            listDataHeader.add(drawerItem);

            DrawerParent logoutDrawerItem = new DrawerParent(getString(R.string.logout), "",ICON_DRAW_LOGOUT, false, false,
                    false);
            logoutDrawerItem.setData(LOGOUT);
            listDataHeader.add(logoutDrawerItem);
        }
    }

    private void prepareCategory() {
        List<Category> categoryList = getCategoryList();

        for (Category cate : categoryList) {
            int groupIndex = listDataHeader.size();
            boolean expandable = (cate.getChildren() != null);
            int icon_draw=0;
            switch (cate.getId()) {
                case 56:
                    icon_draw=ICON_DRAW_FASHION;
                    break;
                case 179:
                    icon_draw=ICON_DRAW_FASHION_ACCESSORIES;
                    break;
                case 53:
                    icon_draw=ICON_DRAW_FOOD_RESTAURANT;
                    break;
                case 54:
                    icon_draw=ICON_DRAW_HEALTH_BEAUTY;
                    break;
                case 52:
                    icon_draw=ICON_DRAW_TRAVEL_RESORT;
                    break;
                case 55:
                    icon_draw=ICON_DRAW_ENTERTAIMENT_EDUCATION;
                    break;
                case 57:
                    icon_draw=ICON_DRAW_HOUSEHOLD_FURNITURE;
                    break;
                case 59:
                    icon_draw=ICON_DRAW_ELECTRONIC_TECHNOLOGY;
                    break;
                case 68:
                    icon_draw=ICON_DRAW_MOTHER_BABY;
                    break;
            }
            DrawerParent parent = new DrawerParent(cate.getName(), "", icon_draw, false, false, expandable);
            parent.setData(cate);
            listDataHeader.add(parent);

            if (cate.getChildren() != null) {
                List<DrawerChild> subCate = new ArrayList<DrawerChild>();
                for (Category sub : cate.getChildren()) {
                    subCate.add(new DrawerChild(sub.getName(), false, sub));
                }
                listDataChild.put(groupIndex, subCate);
            }
        }
    }

    private void prepareLocation() {
        List<City> cityList = getCityList();

        City currentCity = getCurrentCity();

        listDataHeader.add(new DrawerParent(currentCity.getName(), "",
                R.drawable.ic_location, false, false, true));

        List<DrawerChild> locations = new ArrayList<DrawerChild>();

        for (City city : cityList) {
            if (city.getId() != currentCity.getId())
                locations.add(new DrawerChild(city.getName(), false, city,R.drawable.ic_location));
        }
        listDataChild.put(0,locations);
    }

    @Override
    public void onEditProfile() {

    }

    @Override
    public void onSearchRequested(String query) {

    }
}
